//
//  QLMKeySearchViewController.m
//  Demo
//
//  Created by 龚碧容 on 2017/9/20.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "QLMKeySearchViewController.h"

static NSString *cellId=@"searchCellId";

@interface QLMKeySearchViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UISearchBar *keySearch;

@property (nonatomic,strong)NSMutableArray *resultDataArray;
@property (nonatomic,strong)NSMutableArray *sourceDataArray;

@end

@implementation QLMKeySearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor greenColor];
    self.title=@"地址关键字搜索";
    
    self.resultDataArray=[NSMutableArray array];
    self.sourceDataArray=[NSMutableArray arrayWithObjects:@"张三",@"张三无",@"张我",@"张欧尼",@"李三",@"李三阿萨",@"王三", nil];
    
    _tableView=[[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    
    _keySearch=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 50)];
    _keySearch.delegate=self;
    _tableView.tableHeaderView=_keySearch;
    _tableView.tableFooterView=[UIView new];
}

- (void)returnText:(KeySearchBlock)block{
    self.block=block;
}

#pragma mark - UITableView datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.resultDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    
    cell.textLabel.text=self.resultDataArray[indexPath.row];
    
    return cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [_keySearch resignFirstResponder];
    _keySearch.showsCancelButton=NO;
    
    self.block(self.resultDataArray[indexPath.row]);
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UISearchBar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton=YES;
    
    for(id cc in [searchBar.subviews[0] subviews]){
        if([cc isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    [self.resultDataArray removeAllObjects];
    
    for (int i = 0; i < self.sourceDataArray.count; i++) {
        NSString *string = self.sourceDataArray[i];
        if (string.length >= searchText.length) {
            if([self.sourceDataArray[i] rangeOfString:searchText].location !=NSNotFound){
                [self.resultDataArray addObject:self.sourceDataArray[i]];
            }
        }
    }
    
    [_tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    searchBar.showsCancelButton=NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    searchBar.showsCancelButton=NO;
    NSLog(@"searchBar.text = %@",searchBar.text);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
